package com.example.newproject_overseas.ViewModel

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import java.io.File

class ImageViewModel : ViewModel()  {

    var idcardfront_ImageBS = mutableStateOf<ImageBitmap?>(null)
    var idcardback_ImageBS  = mutableStateOf<ImageBitmap?>(null)
    var panCard_ImageBS     = mutableStateOf<ImageBitmap?>(null)

    fun loadImageBitmapFromFile(filePath: String,oper:Int) {
        viewModelScope.launch {
            val file = File(filePath)
            if (file.exists()) {
                val options = BitmapFactory.Options()
                options.inPreferredConfig = Bitmap.Config.ARGB_8888
                val bitmap = BitmapFactory.decodeFile(file.absolutePath, options)
                if(oper == 0){
                    idcardfront_ImageBS.value = bitmap?.asImageBitmap()
                }else if(oper == 1){
                    idcardback_ImageBS.value  = bitmap?.asImageBitmap()
                }else if(oper == 2){
                    panCard_ImageBS.value     = bitmap?.asImageBitmap()
                }
            }
        }
    }
}